/*
 * Date：10/23/18 10:08 PM
 * Author: guomw
 *
 */

package org.elanfox.intellicommunity.biz.repository.equipment;

import org.elanfox.intellicommunity.biz.entity.equipment.BaseStationPO;
import org.elanfox.intellicommunity.data.base.base.JpaSearchableRepository;
import org.elanfox.intellicommunity.msg.ienum.BaseStationType;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * 基站Repository
 *
 * @author loutianxi
 * @date 2019-12-04
 */
@Repository
public interface BaseStationRepository extends JpaSearchableRepository<BaseStationPO, Integer> {

    /**
     * 更新启用状态
     *
     * @param baseStationId
     * @param enable
     * @return
     */
    @Query("update BaseStationPO c set c.enable=?2 where c.baseStationId=?1")
    @Modifying(clearAutomatically = true)
    @Transactional(rollbackFor = RuntimeException.class)
    int updateEnableById(Integer baseStationId, boolean enable);

    /**
     * 删除基站
     *
     * @param no
     * @return
     */
    @Query("delete from BaseStationPO c  where c.no=?1")
    @Modifying(clearAutomatically = true)
    @Transactional(rollbackFor = RuntimeException.class)
    int deleteByNo(Long no);

    /**
     * 更新防拆卸状态
     *
     * @param baseStationId
     * @param dismantleStatus
     * @return
     */
    @Query("update BaseStationPO c set c.dismantleStatus=?2 where c.baseStationId=?1")
    @Modifying(clearAutomatically = true)
    @Transactional(rollbackFor = RuntimeException.class)
    int updateDismantleStatusById(Integer baseStationId, boolean dismantleStatus);

    /**
     * 根据no查询基站
     *
     * @param no
     * @return
     */
    BaseStationPO findByNo(Long no);

    BaseStationPO findByDeviceAddrAndStationType(Long deviceAddr, BaseStationType stationType);

    long countAllByOnlineStatusIsTrueAndEnableIsTrue();

    long countAllByOnlineStatusIsFalseAndEnableIsTrue();
}
